package com.hdoit.kylin.web.group.providers;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.PageContext;
import com.hdoit.kylin.logical.relative.service.IProjectUserService;
import com.hdoit.kylin.logical.user.model.Linker;
import com.hdoit.kylin.logical.user.service.IContactService;
import com.hdoit.kylin.logical.user.service.IStudentService;
import com.hdoit.kylin.logical.user.service.ITeacherService;
import com.tennetcn.common.aop.CommonApplicationContextUtil;
import com.tennetcn.common.message.ListItem;
import com.tennetcn.common.utils.PageContextUtils;
import com.tennetcn.component.attribute.provider.IMultipleOptionListProvider;
import com.tennetcn.component.web.SessionWebFactory;
import com.tennetcn.plugins.devshorts.enumerable.Enumerable;

public class PersonListProvider implements IMultipleOptionListProvider<ListItem>{
	private List<ListItem> dataList;

	@Override
	public List<ListItem> getDataList(Object viewData){
		SessionWebFactory sessionWebFactory = CommonApplicationContextUtil.getCurrentContext().getBean(SessionWebFactory.class);
		ITeacherService teacherService = CommonApplicationContextUtil.getCurrentContext().getBean(ITeacherService.class);
		IStudentService studentService = CommonApplicationContextUtil.getCurrentContext().getBean(IStudentService.class);
		String groupId = null;
		groupId = teacherService.queryGroupIdByLoginId(sessionWebFactory.getCurrentLogin().getLoginId());	
		if(groupId==null)
			groupId = studentService.queryGroupIdByLoginId(sessionWebFactory.getCurrentLogin().getLoginId());
		List<Linker> linkers = getLinkers(groupId);
		if(linkers == null ||linkers.size()<=0)
			return null;
		dataList = new ArrayList<ListItem>(); 
		ListItem listItem  =null;
		for(Linker linker : linkers){
			listItem = new ListItem();
			listItem.setText(linker.getContactName());
			listItem.setValue(linker.getId());
			dataList.add(listItem);
		}
		return dataList;
	}
	
	private List<Linker> getLinkers(String groupId) {
		IContactService contactService = CommonApplicationContextUtil.getCurrentContext().getBean(IContactService.class);
		return contactService.queryLinkersByGroupIdNoContact(groupId);
	}
	
	@Override
	public List<ListItem> getSelectedItems(Object value, Object viewData){
		PageContext pageContext = (PageContext)viewData; 
		String projectId = PageContextUtils.getParam(pageContext, "key");
		List<String> selectLinkerIds = querySelectLinkerIds(projectId);
		if(selectLinkerIds == null || selectLinkerIds.size() <= 0){
			return null;
		}
		List<ListItem> selDataList = new ArrayList<ListItem>();
		for(String linkerId : selectLinkerIds){
			ListItem listItem = Enumerable.init(dataList).filter(data->linkerId.equals(data.getValue())).first();
			if(listItem!=null){
				selDataList.add(listItem);
			}
		}
		return selDataList;
	}
	private List<String> querySelectLinkerIds(String projectId) {
		IProjectUserService projectUserService = CommonApplicationContextUtil.getCurrentContext().getBean(IProjectUserService.class);		
		return projectUserService.queryListByProjectId(projectId);
	}
}
